from datetime import date, datetime

from django.http import HttpResponse
from django.utils.timezone import make_aware

from article.models import User, Article, UserExtends


# Create your views here.

def add_article(request):
    user = User.objects.get(username="haichenyi")
    if user is None:
        user = User(username="haichenyi", password="123456")
        user.save()
    article = Article.objects.create(title="水浒传1", content="一百单八将1", author=user)
    article.save()
    return HttpResponse("新增成功")


def deleted_article(request):
    article = Article.objects.get(title="红楼梦")
    article.delete()
    return HttpResponse("删除成功")


def update_article(request):
    user = User.objects.get(username="jack")
    if user is None:
        user = User(username="jack", password="123456")
        user.save()
    article = Article.objects.get(title="红楼梦")
    article.author = user
    article.save()
    return HttpResponse("修改成功")


def query_article(request):
    article = Article.objects.get(title="红楼梦")
    print(f"{article.title}, {article.content}, {article.author.username}")
    return HttpResponse("查询成功")


def get_user_article(request):
    user = User.objects.get(username="haichenyi")
    # articles = user.article_set.all() # 默认获取用户下的所有文章
    # articles = user.articles.all() # 获取用户下的所有文章
    # __contains：包含
    articles = user.articles.filter(title__contains="1").all()
    for article in articles:
        print(article.title, article.content, article.author.username)
    return HttpResponse("查询成功")


def add_user_extends_info(request):
    user = User.objects.get(username="haichenyi")
    if user is None:
        user = User(username="haichenyi", password="123456")
        user.save()
    user_extends = UserExtends.objects.create(user=user, address="上海市闵行区", school="北京大学",
                                              birthday=date(1990, 5, 15))
    user_extends.save()
    return HttpResponse("添加成功")


def query_test1(request):
    user1 = User.objects.filter(username="haichenyi")  # 字段等于，"username" = haichenyi
    print(user1.query)  # 查看底层执行的sql语句
    for user in user1:
        print(user.id, user.username)
    user2 = User.objects.filter(username__contains="hai")  # 字段包含 "username" LIKE %hai%
    print("__contains", user2.query)  # 查看底层执行的sql语句
    for user in user2:
        print(user.id, user.username)
    user2 = User.objects.filter(username__icontains="hai")
    print("__icontains", user2.query)  # 查看底层执行的sql语句
    for user in user2:
        print(user.id, user.username)
    return HttpResponse("查询成功")


def query_test2(request):
    user2 = User.objects.filter(username__exact="haichenyi")  # username="hai"意义相同,exact:准确查找.
    print("__exact", user2.query)  # 查看底层执行的sql语句
    for user in user2:
        print(user.id, user.username)
    user2 = User.objects.filter(username__iexact="haiChenyi")  # iexact:忽略大小写."username" LIKE haiChenyi
    print("__iexact", user2.query)  # 查看底层执行的sql语句
    for user in user2:
        print(user.id, user.username)
    return HttpResponse("查询成功")


def query_test3(request):
    user2 = User.objects.filter(id__in=[1, 2, 3])  # "id" IN (1, 2, 3).
    print("__in", user2.query)  # 查看底层执行的sql语句
    for user in user2:
        print(user.id, user.username)
    return HttpResponse("查询成功")


def query_test4(request):
    start_time = make_aware(datetime(year=2025, month=5, day=28))
    end_time = make_aware(datetime(year=2025, month=5, day=30))
    user2 = User.objects.filter(create_time__range=(start_time, end_time))  # "create_time" BETWEEN 2025-05-27 16:00:00 AND 2025-05-29 16:00:00
    print("__range", user2.query)  # 查看底层执行的sql语句
    for user in user2:
        print(user.id, user.username)
    return HttpResponse("查询成功")

def query_test5(request):
    # 查找作者，并且文章标题中包含1
    user2 = User.objects.filter(articles__title__icontains="1") # FROM "article_user" INNER JOIN "article_article" ON ("article_user"."id" = "article_article"."author_id") WHERE "article_article"."title" LIKE %1%
    print("关联查询:", user2.query)  # 查看底层执行的sql语句
    for user in user2:
        print(user.id, user.username)
    return HttpResponse("查询成功")
